package com.jzt.gateway.service;

import com.jzt.gateway.config.AuthConfig;
import com.jzt.gateway.config.ServersConfig;
import com.jzt.gateway.dao.AccountDao;
import com.jzt.gateway.dao.AccountRegisterDao;
import com.jzt.gateway.dao.UnionUserDao;
import com.jzt.gateway.dto.request.AccountRegisterReq;
import com.jzt.gateway.entity.Account;
import com.jzt.gateway.entity.AccountRegister;
import com.jzt.gateway.entity.UnionUser;
import com.jzt.gateway.utils.CryptoUtils;
import com.jzt.gateway.utils.EncodeDecodeUtils;
import com.jzt.gateway.utils.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作者：lizw <br/>
 * 创建时间：2020/06/18 14:20 <br/>
 */
@Service
@Slf4j
public class RegisterService {
    @Autowired
    private ServersConfig serversConfig;
    @Autowired
    private UnionUserDao unionUserDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private AccountRegisterDao accountRegisterDao;

    /**
     * 用户注册 pre
     */
    @Transactional
    public Account preRegister(AccountRegisterReq accountRegisterReq, AuthConfig authConfig) {
        // 请求参数校验
        checkAccountRegisterReq(accountRegisterReq);
        // 创建OneId
        final String oneId = createOneId();
        // 新增 UnionUser
        UnionUser unionUser = new UnionUser();
        unionUser.setOneId(oneId);
        // unionUser.setLoginPwd(accountRegisterReq.getLoginPwd());
        // unionUser.setTelephone(accountRegisterReq.getTelephone());
        // unionUser.setEmail(accountRegisterReq.getEmail());
        unionUserDao.insert(unionUser);
        // 新增 Account
        Account account = new Account();
        account.setOneId(oneId);
        account.setPlatform(authConfig.getRouteId());
        // account.setUserAgentId();
        account.setLoginName(accountRegisterReq.getLoginName());
        if (StringUtils.isNotBlank(accountRegisterReq.getLoginPwd())) {
            // 密码加密存储
            final byte[] key = EncodeDecodeUtils.decodeHex(serversConfig.getPasswordSecretKey());
            final byte[] iv = EncodeDecodeUtils.decodeHex(serversConfig.getPasswordSecretIv());
            final String loginPwd = EncodeDecodeUtils.encodeBase64(CryptoUtils.aesEncrypt(accountRegisterReq.getLoginPwd().getBytes(), key, iv));
            account.setLoginPwd(loginPwd);
        }
        account.setTelephone(accountRegisterReq.getTelephone());
        accountDao.insert(account);
        // 新增 AccountRegister
        AccountRegister accountRegister = new AccountRegister();
        accountRegister.setOneId(oneId);
        accountRegister.setPlatform(account.getPlatform());
        // accountRegister.setUserAgentId();
        // accountRegister.setRetryCount();
        // accountRegister.setLastRetryTime();
        accountRegister.setHttpMethod(accountRegisterReq.getHttpMethod());
        accountRegister.setHttpQueryStr(accountRegisterReq.getHttpQueryStr());
        accountRegister.setHttpBody(accountRegisterReq.getHttpBody());
        accountRegisterDao.insert(accountRegister);
        return account;
    }

    /**
     * 用户注册 post(回写user_agent_id值)
     */
    @Transactional
    public void postRegister(Account account) {
        accountDao.setUserAgentId(account.getOneId(), account.getUserAgentId());
        accountRegisterDao.setUserAgentId(account.getOneId(), account.getUserAgentId());
    }

    public void checkAccountRegisterReq(AccountRegisterReq accountRegisterReq) {
        // 参数校验

        // 重复注册校验
    }

    /**
     * 生成用户统一ID
     */
    protected String createOneId() {
        return "u" + SnowFlake.SNOW_FLAKE.nextId();
    }
}
